<?php

class Page {
    public $startPage;//显示起始页
    public $endPage;//显示最后一页
    public $nowPage;//当前页
    public $lastPage;//最后一页
    public $showNums = 8;//显示的数量
    public $pageSize;//每页显示数量
    public $totalPages;//总页数
    public $total;//总数据量
    /**
     +----------------------------------------------------------
     * 架构函数
     +----------------------------------------------------------
     * @access public
     +----------------------------------------------------------
     * @param array $totalRows  总的记录数
     * @param array $listRows  每页显示记录数
     * @param array $parameter  分页跳转的参数
     +----------------------------------------------------------
     */
    public function __construct($total,$pageSize,$parameter='') {
        $this->total = $total;//总数据量
        $this->pageSize = !empty($pageSize) ? $pageSize : PAGE_SIZE;//每页显示数量
        $this->totalPages = ceil( $this->total / $this->pageSize );     //总页数
        $this->nowPage  = !empty($_GET['p']) ? intval($_GET['p']) : 1;

        if(!empty($this->totalPages) && $this->nowPage>$this->totalPages) {
            $this->nowPage = $this->totalPages;
        }
        $this->startPage = (ceil($this->nowPage/$this->showNums) -1) * $this->showNums + 1;
        $this->endPage = $this->startPage + $this->showNums - 1;
        if ($this->endPage > $this->totalPages) {
            $this->endPage = $this->totalPages;
        }
    }
    /**
     * 样式一
     */
    public function style1(){
        if ($this->totalPages <=1) return;
        $pageStr = '<style>.pagination {
                        padding-left: 0;
                        margin: 10px 0;
                        border-radius: 4px;
                      }

                      .pagination li {
                        float:left;
                        margin-left:4px;
                      }

                      .pagination a{
                        padding: 6px 12px;
                        margin-left: -1px;
                        text-decoration: none;
                        background-color: #ffffff;
                        border: 1px solid #dddddd;
                      }

                      .pagination a {
                        margin-left: 0;
                        border-bottom-left-radius: 4px;
                        border-top-left-radius: 4px;
                      }

                      .pagination a {
                        border-top-right-radius: 4px;
                        border-bottom-right-radius: 4px;
                      }

                      .pagination a:hover{
                        background-color: #eeeeee;
                      }

                      .active a{
                        color: #ffffff;
                        cursor: hand;
                        font-weight :bold;
                        background-color: #5a85b2!important;
                        border-color: #428bca!important;
                      }
                      </style>';
        $pageStr .= '<ul class="pagination">';
        if ($this->nowPage > $this->showNums) {
            $pageStr .= '<li><a href="'.$this->url($this->nowPage-$this->showNums).'">«</a></li>';
        } else {
            $pageStr .= '<li><a href="'.$this->url(1).'">«</a></li>';
        }
        if ($this->nowPage > 1) {
            $pageStr .= '<li><a href="'.$this->url($this->nowPage-1).'">‹</a></li>';
        }
        $start = $this->startPage;
        for($start; $start<=$this->endPage; $start++) {
            $pageStr .= '<li '.($this->nowPage==$start ? 'class="active"' :'').'><a href="'.($this->url($start)).'">'.$start.($this->nowPage==$start ? '' :'').'</a></li>';
        }
        if ($this->nowPage < $this->totalPages) {
            $pageStr .= '<li><a href="'.$this->url($this->nowPage+1).'">›</a></li>';
        }
        ;
        if ($this->endPage != $this->totalPages) {
            $page = $this->nowPage+$this->showNums;
            if ($page > $this->totalPages) $page = $this->totalPages;
            $pageStr .= '<li><a href="'.$this->url($page).'">»</a></li>';
        }
        $pageStr .= '</ul>';
        return $pageStr;
    }

    /**
     * 根据路由规则拼接url
     */
    private function url($p=1){
            $uri = $_SERVER['REQUEST_URI'].(strpos($_SERVER['REQUEST_URI'],'?')?'':"?");
            $uri .= empty($_SERVER['QUERY_STRING']) ? '' : '&';
            if (stristr($uri,'p=')) {
                $uri = preg_replace('/p=\d{1,}&{0,}/','p='.$p, $uri);
            } else {
                $uri .= 'p='.$p;
            }
        return $uri;
    }
}
?>